<?php

class Member_Withdrawal_model extends CI_Model
{

    // 入库申请操作
    public function addWithdrawal($code, $data)
    {
        $this->db->insert("1_form_{$code}", $data);
        $oid = $this->db->insert_id();
        if ($this->db->insert("1_form_{$code}_data_0", ['id' => $oid, 'uid' => $data['uid']])) {
            return $oid;
        } else {
            return false;
        }
    }

    // 操作申请 : 审核
    public function operationApply($code, $param, $data)
    {
        return $this->db->set($data)->where($param)->update("1_form_{$code}");
    }

    // 返回申请列表
    public function applyList($code, $param, $page, $pageSize)
    {
        return $this->db->select('id,account,money,status,real_uid,reason,inputtime')->where($param)
            ->limit($pageSize, ($page - 1) * $pageSize)
            ->order_by('id desc')->get("1_form_{$code}")->result_array();
    }

    public function transferApply($item)
    {
        if ($item['inputtime']) {
            $item['inputtime'] = date('Y-m-d H:i', $item['inputtime']);
        }
        return $item;
    }

    // 返回已申请,待审核金额
    public function waitCheck($code, $uid)
    {
        return $this->db->select("sum(price) as price")->where(['uid' => $uid, 'status' => 0])
            ->get("1_form_{$code}")->row()->money;
    }

    // 获取本日提现总额数
    public function todayAmount($uid, $range)
    {
        return $this->db->select("SUM(money) as total")->where([
            'status' => 2,
            'uid' => $uid,
            'inputtime >' => $range['start'],
            'inputtime <' => $range['end']
        ])->get('1_form_apply')->row()->total;
    }

    // 添加收支记录
    public function addPayLog($data)
    {
        $this->db->insert('member_payrecord', [
            'uid' => (int)$data['uid'],
            'value' => $data['value'],
            'module' => $data['module'],
            'note' => $data['note'] ?: '消费',
            'inputtime' => SYS_TIME,
            'pid' => $data['pid'] ?: 0
        ]);
        return $this->db->insert_id() ?: false;
    }

    // 用户的流水记录(指成功的记录)
    public function memberPayLog($uid, $page, $pageSize)
    {
        return $this->db->select('id,value,module,note,inputtime')
            ->where(['uid' => $uid])
            ->limit($pageSize, ($page - 1) * $pageSize)
            ->order_by('id desc')
            ->get('member_payrecord')->result_array();
    }

    public function payLogTransfer($item = [])
    {
        if ($item['inputtime']) {
            $item['inputtime'] = date('Y-m-d H:i', $item['inputtime']);
        }
        return $item;
    }


    //  我的团队列表
    public function myTeam($code, $page, $pageSize)
    {
        if (empty($code)) {
            return [];
        }
        return $this->db->select("uid,name,avatar,team_time,spend")
            ->where(['team_code' => $code])->limit($pageSize, ($page - 1) * $pageSize)
            ->get('member')->result_array();
    }

    public function myTeamTransfer($item = [])
    {
        if ($item['team_time']) {
            $item['team_time'] = date('Y-m-d H:i', $item['team_time']);
        }
        if ($item['avatar']) {
            $item['avatar'] = dr_thumb($item['avatar'], 80, 80);
        }
        if ($item['spend']) {
            $item['spend'] = $item['spend'] > 0 ? '已消费' : '未消费';
        }
        return $item;
    }
}